package com.jm.weibo.ui;

import java.util.HashMap;
import java.util.List;

import weibo4andriod.Comment;
import weibo4andriod.Status;
import weibo4andriod.Trends;

import com.jm.weibo.logic.IWeiboActivity;
import com.jm.weibo.logic.ListCommentAdapter;
import com.jm.weibo.logic.ListTrendsAdapter;
import com.jm.weibo.logic.MainService;
import com.jm.weibo.logic.StatusAdapter;
import com.jm.weibo.logic.Task;
import com.jm.weibo.util.AppContainer;
import com.jm.weibo.util.MyToastUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
/**
 * 主题列表
 * @author Administrator
 *
 */
public class ListMyTopicsActivity extends Activity implements IWeiboActivity{

	   
	public static final String REF_MYTOPIC="REF_MYTOPIC";//更新微博 
    
    public static final String REF_MYTOPIC_MORE="REF_MYTOPIC_MORE";//更多信息
    
	private View vTitle;
	
	private Button btnReturn;

	private Button btnHome;
	
	private TextView tvTitle;
	
	private Status status;
	
	private ListView lv;
	
	private View process;
	
	public int pagesize=5;//每页条数
	public int nowpage=1;//页数
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//获取布局，默认标题为:没有
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.list_my_topics);
		HashMap hm=new HashMap();
		Task tsListTrends=new Task(Task.TASK_GET_MY_TOP,hm);
	    MainService.addNewTask(tsListTrends);
		setContentView(R.layout.lsit_comments);
		
		this.initControlsAndAddControlsEvent();
	}
	
	private void initControlsAndAddControlsEvent() {
		
		 this.vTitle = this.findViewById(R.id.title4ListMyTopics);
		 this.tvTitle = (TextView) this.vTitle.findViewById(R.id.textView);
		 this.btnReturn = (Button) this.vTitle.findViewById(R.id.title_bt_left);
		 this.btnHome = (Button) this.vTitle.findViewById(R.id.title_bt_right);

		 this.btnReturn.setText(R.string.imageviewer_back);
		 this.btnHome.setBackgroundResource(R.drawable.title_home);
		 this.tvTitle.setText(R.string.topic);
		 //控件初始化
		//请求当前用户的话题 
			HashMap hm=new HashMap();
			hm.put("pagesize", pagesize);
			hm.put("nowpage", nowpage);
			//请求当前用户的微博主页 
			Task tsMine=new Task(Task.TASK_GET_MY_TOP,hm);
		    MainService.addNewTask(tsMine);		    
		    MainService.allActivity.add(this);
		 
		 btnReturn.setOnClickListener(new OnClickListener()
		    {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(ListMyTopicsActivity.this,UserInfoActivity.class);
					startActivity(intent);
				}    
			 
		    });
		 btnHome.setOnClickListener(new OnClickListener()
		    {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(ListMyTopicsActivity.this,HomeActivity.class);
					startActivity(intent);
				}    
			 
		    });
		  //获取列表对象和进度条对象
		    process=this.findViewById(R.id.progress);
		    lv=(ListView)this.findViewById(R.id.freelook_listview);
		    lv.setClickable(true);	
		    lv.setOnItemClickListener(new OnItemClickListener(){
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					MyToastUtil.myToastShow(ListMyTopicsActivity.this, "查看");
					if(arg3==-1){  //更多
						
							nowpage++;
							HashMap hm=new HashMap();
							hm.put("pagesize", pagesize);
							hm.put("nowpage", nowpage);
							Task tsMine=new Task(Task.TASK_GET_MORE_WEIBO_USER,hm);
							MainService.addNewTask(tsMine);
						
					}
					 if(arg3==0)
					{//刷新
						HashMap hm=new HashMap();
						hm.put("pagesize", pagesize);
						hm.put("nowpage", nowpage);
				    	Task tsMine=new Task(Task.TASK_GET_WEIBO_USER,hm);
				    	MainService.addNewTask(tsMine);
					}
					 if(arg3!=-1 && arg3!=0){

						 
					    Intent it=new Intent(ListMyTopicsActivity.this,  ViewWeiboActivity.class);
					    try{
						    ListTrendsAdapter ma=(ListTrendsAdapter)lv.getAdapter();
						    Trends trends=(Trends)ma.trendsList.get(arg2-1);
						    it.putExtra("trends",trends);
					    }catch(Exception e){
					    	Log.e("e","error"+e);
					    }				    
					    ListMyTopicsActivity.this.startActivityForResult(it, 0);
					}
					
				}
		    	
		    });
		    //注册上下文菜单
	        this.registerForContextMenu(lv);
		 
	}
	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		Log.d("ref", (String)param[0]);
		String flag = (String)param[0];
		if(flag.equals("REF_MYTOPIC"))
	    {
			List<Trends> alls=(List<Trends>)param[1];
			ListTrendsAdapter ma=new ListTrendsAdapter(this,alls);
			lv.setAdapter(ma);//更新列表
			process.setVisibility(View.GONE);
		}
		
		else if(flag.equals(REF_MYTOPIC_MORE)){
			List<Trends> alls=(List<Trends>)param[1];
			((ListTrendsAdapter)lv.getAdapter()).addMoreData(alls);
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode){
	      case RESULT_OK: /* 取得数据，并显示于画面上 */
	         Bundle bunde = data.getExtras();
	         String newBolgResultMsg = bunde.getString("newBlogResult");
	         Toast.makeText(this, newBolgResultMsg, Toast.LENGTH_SHORT).show();
	         break;
	         
	      default:
	        break;
	    }
	}

		
	}




